/* SPDX-License-Identifier: GPL-2.0+
 *
 * (C) Copyright 2020
 * Anton Puiu, anton.puiu@email.com
 */

#ifndef ERROR_H
#define ERROR_H

#include "so_functions.h"

#define FILE_NOT_FOUNT_ERROR \
"File does not exist.\n"
#define FILE_CANNOT_READ_ERROR \
"File cannot be readed.\n"
#define FILE_CANNOT_WRITE_ERROR \
"File cannot be writed.\n"
#define FILE_STAT_ERROR \
"Error retriving file stat.\n"
#define FILE_READ_OVERFLOW \
"Not enough bytes in file.\n"

#define LSEEK_ERROR \
"Error in lseek.\n"
#define READ_FROM_FILE_ERROR \
"Error reading from file.\n"
#define FFLUSH_ERROR \
"Error writing buffer data to file.\n"

#define SO_FILE_MEMORY_ERROR \
"Error alocating memory for SO_FILE. Exitting.\n"
#define SO_FILE_BUFFER_MEMORY_ERROR \
"Error alocating memory for buffer inside of SO_FILE. Exitting.\n"

#define READ_MODE_SELECTED \
"Read mode selected.\n"
#define WRITE_MODE_SELECTED \
"Write mode selected.\n"
#define READ_WRITE_MODE_SELECTED \
"Read and write mode selected.\n"
#define APPEND_MODE_SELECTED \
"Append mode selected.\n"
#define APPEND_READ_MODE_SELECTED \
"Append and read mode selected.\n"
#define UNKNOWN_MODE_SELECTED \
"Unknown mode selected.\n"

#define OPEN_FILE_ERROR \
"Error opening file. Exitting.\n"
#define CREATE_FILE_ERROR \
"Error creating file. Exitting.\n"
#define UNKNOWN_ERROR \
"Unknown error occured. Exitting.\n"

#define FILE_NOT_FOUND_CREATE \
"File does not exist, so i will create it.\n"
#define TRUNC_EXISTING_FILE \
"File exists, so i will truncate it.\n"
#define FILE_OPEN_APPEND \
"File exists, so i will open it.\n"

static inline void *error_allocate_file(void)
{
	#ifdef DEBUG
	printf(SO_FILE_MEMORY_ERROR);
	#endif

	return NULL;
}

static inline void *error_allocate_buffer(SO_FILE *file)
{
	#ifdef DEBUG
	printf(SO_FILE_BUFFER_MEMORY_ERROR);
	#endif

	so_free(file, NULL);

	return NULL;
}

static inline void *error_open_file(SO_FILE *file, void *buffer)
{
	#ifdef DEBUG
	printf(OPEN_FILE_ERROR);
	#endif

	so_free(buffer, file, NULL);

	return NULL;
}

static inline void *error_create_file(SO_FILE *file, void *buffer)
{
	#ifdef DEBUG
	printf(CREATE_FILE_ERROR);
	#endif

	so_free(buffer, file, NULL);

	return NULL;
}

static inline void *error_file_stat(SO_FILE *file, void *buffer)
{
	#ifdef DEBUG
	printf(FILE_STAT_ERROR);
	#endif

	so_free(buffer, file, NULL);

	return NULL;
}

static inline int error_read_from_file(void)
{
	#ifdef DEBUG
	printf(READ_FROM_FILE_ERROR);
	#endif

	return 0;
}

static inline int error_lseek(void)
{
	#ifdef DEBUG
	printf(LSEEK_ERROR);
	#endif

	return 0;
}

static inline int error_fflush(void)
{
	#ifdef DEBUG
	printf(FFLUSH_ERROR);
	#endif

	return 0;
}

static inline void *error_read_overflow(SO_FILE *file, void *buffer)
{
	#ifdef DEBUG
	printf(FILE_READ_OVERFLOW);
	#endif

	return so_free(buffer, file, NULL);
}

static inline void *error_unknown(SO_FILE *file, void *buffer)
{
	#ifdef DEBUG
	printf(UNKNOWN_ERROR);
	#endif

	so_free(buffer, file, NULL);

	return NULL;
}

#endif
